import { createStore } from 'vuex'
import { allCompanyAPI} from '@/api'
// 持久化数据
import createPersistedState from 'vuex-persistedstate'
export default createStore({
  state: {
    token:'',
    user:{},
    isCollapsed:false,
    companyData:{},
  },
  getters: {
  },
  mutations: {
    updateToken(state,value){
      state.token=value
    },
    // 获取用户
    getuser(state,value){
      state.user={
        ...state.user,
        ...value
      }
    },
    // 更新用户信息
    updateUser(state,value){
      state.user={
        ...state.user,
        ...value
      }
    },
    // 清除用户信息
    clearuser(state,value){
      state.user={}
    },
    // 控制侧边栏的展开
    changeCollapsed(state){
      state.isCollapsed=!state.isCollapsed
    },
    // 获取公司信息
    getCompany(state, company) {
      state.companyData=company
    },
    // 更新公司信息
    updateCompany(state,value) {
      state.companyData={
        ...state.companyData,
        ...value
      }
    }
  },
  actions: {
    // 获取公司信息
     async getAllCompany(context){
      const {data:res}=await allCompanyAPI()
      if(res.status===0) context.commit("getCompany", res.companyinfo[0])
  }
  },
  modules: {
  },
  plugins: [
    createPersistedState() // 注入持久化插件
  ]
  // plugins:[createPersistedState({
  //   paths:['isCollapsed','/login']  //控制是否持久化
  // })]
})
